package com.offer;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 18:21
 */
public class offer33 {
    public boolean verifyPostorder(int[] postorder) {
        return check(postorder,0,postorder.length-1);
    }


    public boolean check(int[] ordre, int star, int end) {
        if (star > end) {
            return false;
        }

        int rootVal = ordre[end];
        int left = star;
        while (ordre[left] < rootVal) {
            left++;
        }
        int right = left;
        while (ordre[right] > rootVal) {
            right++;
        }
        if (right != end) {
            return false;
        }
        return check(ordre, star, left - 1) && check(ordre, left, right);
    }
}
